﻿using System;
using Ornament.Domain;

namespace Products
{
    [Serializable]
    public class ProductVersion : DomainObject<Guid>
    {
        protected ProductVersion()
        {
        }

        internal ProductVersion(string versionName, double order, Product product)
        {
            OrderValue = order;
            Name = versionName;
            Product = product;
        }

        public virtual Product Product { get; private set; }

        public virtual double OrderValue { get; private set; }

        public virtual string Name { get; private set; }

        public override string ToString()
        {
            return Name;
        }

        public static bool operator >(ProductVersion a, ProductVersion b)
        {
            return a.OrderValue > b.OrderValue;
        }

        public static bool operator <(ProductVersion a, ProductVersion b)
        {
            return b > a;
        }

        public static bool operator <=(ProductVersion a, ProductVersion b)
        {
            return a.OrderValue <= b.OrderValue;
        }

        public static bool operator >=(ProductVersion a, ProductVersion b)
        {
            return a.OrderValue >= b.OrderValue;
        }

        public static bool operator ==(ProductVersion a, ProductVersion b)
        {
            if (!object.ReferenceEquals(b, null))
                return b.Equals(a);
            return object.ReferenceEquals(a, null) || a.Equals(b);
        }

        public static bool operator !=(ProductVersion a, ProductVersion b)
        {
            return !(a == b);
        }

        protected override int CreateHashCode()
        {
            return Id == Guid.Empty ? (OrderValue + Name).GetHashCode() : Id.GetHashCode() * 2;
        }

        public virtual bool Equals(ProductVersion other)
        {
            if (ReferenceEquals(null, other)) return false;
            if (ReferenceEquals(this, other)) return true;
            return Equals(other.Product, Product) && other.OrderValue.Equals(OrderValue) && Equals(other.Name, Name);
        }

        public override int GetHashCode()
        {
            unchecked
            {
                int result = base.GetHashCode();
                result = (result * 397) ^ OrderValue.GetHashCode();
                result = (result * 397) ^ (Name != null ? Name.GetHashCode() : 0);
                return result;
            }
        }

        public override bool Equals(object obj)
        {
            if (ReferenceEquals(null, obj)) return false;
            if (ReferenceEquals(this, obj)) return true;
            if (obj.GetType() != typeof(ProductVersion)) return false;
            return Equals((ProductVersion)obj);
        }
    }
}